package com.easy.common.web;

import com.easy.common.jdbc.PageInfo;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Userinfo: guyu
 * Date: 13-1-12
 * Time: 下午3:08
 * To change this template use File | Settings | File Templates.
 */
public class DataGridInfo {
    public static final String SORT = "iSortCol_0";
    public static final String ORDER = "sSortDir_0";
    public static final String ROWS = "iDisplayLength";
    public static final String PAGE = "iDisplayStart";
    public static final String COLUMN_NAME = "sColumnName";

    private String sortId = "oid";

    private String order = "desc";

    private String rows = "10";

    private String page = "0";

    public DataGridInfo(HttpServletRequest request) {
        String sortId = request.getParameter(SORT);
        String columnName = request.getParameter(COLUMN_NAME);
        if (sortId != null && columnName != null) {
            int i = 0;
            try {
                i = Integer.parseInt(sortId);
            } catch (NumberFormatException e) {
              //  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            String[] names = columnName.split(",");
            if (i < names.length) {
                this.sortId = names[i];
            }
        }

        String order = request.getParameter(ORDER);
        if (order != null) {
            this.order = order;
        }

        String row = request.getParameter(ROWS);
        if (row != null) {
            this.rows = row;
        }

        String page = request.getParameter(PAGE);
        if (page != null) {
            this.page = page;
        }
    }

    public String getSortId() {
        return sortId;
    }

    public String getOrder() {
        return order;
    }

    public String getRows() {
        return rows;
    }

    public String getPage() {
        return page;
    }

    public void setSortId(String sortId) {
        this.sortId = sortId;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getStartPage() {
//        int pageInt = 1;
//        int rowsInt = 10;
//
//        try {
//            pageInt = Integer.parseInt(this.page);
//        } catch (NumberFormatException e) {
//        }
//
//        try {
//            rowsInt = Integer.parseInt(this.rows);
//        } catch (NumberFormatException e) {
//        }
//
//        return (pageInt - 1) * rowsInt + "";
        return this.page;
    }

    public PageInfo toPageInfo() {
        return new PageInfo(Integer.parseInt(this.getRows()), Integer.parseInt(this.getPage()), this.getSortId(), this.getOrder());
    }

    public Map<String, String> toMap() {
        Map<String, String> map = new HashMap<String, String>();

        map.put(SORT, this.getSortId());
        map.put(ORDER, this.getOrder());
        map.put(PAGE, this.getPage());
        map.put(ROWS, this.getRows());

        return map;
    }
}
